<?php


namespace app\suguan\controller;


use app\teacher\utils\User as AdminUser;
use think\Db;

class Room extends Base
{
    //根据楼栋id获取宿舍列表
    public function getRoomByHouseId()
    {
        $house_id = request()->param('id', 0, 'intval');
        $list = model('Room')->where('house_id', $house_id)->column('title', 'id');
        return $this->ok($list);
    }

    public function index()
    {
        $page = request()->param('page', 1, 'intval');
        $limit = request()->param('limit', 20, 'intval');
        $room_id = request()->param('room_id', '');
        $search_empty = request()->param('search_empty', ''); //查询空宿舍
        $where = [];
        if ($room_id) {
            $where[] = ['id', '=', $room_id];
        }
        if ($search_empty) {
            $where[] = ['has_people', '=', 0];
        }
        $house_id = $this->house_id();
        $where['house_id'] = ['house_id', '=', $house_id];
        $list = model('Room')->where($where)->page($page, $limit)->select();
        $room = model('Room')->where('house_id', $house_id)->column('title', 'id');
        $total = model('Room')->where($where)->count();
        $data = [
            'list' => $list,
            'total' => $total,
            'room' => $room
        ];
        return $this->ok($data);
    }

    public function detail()
    {
        $id = request()->param('id', 0, 'intval');
        if (!$id) {
            return $this->fail('参数错误');
        }
        $house_id = $this->house_id();
        $detail = model('Room')->where(['house_id' => $house_id, 'id' => $id])->find($id);
        if (empty($detail)) {
            return $this->fail('查无该宿舍信息');
        }
        return $this->ok($detail);
    }

    public function save()
    {
        $params = request()->only(['id', 'title', 'count', 'has_people']);
        $params['house_id'] = $this->house_id();
        try {
            model('Room')->saveData($params);
        } catch (\Exception $e) {
            return $this->fail($e->getMessage());
        }
        return $this->ok('操作成功');
    }

    public function delete()
    {
        $ids = request()->param('id', '');
        try {
            //空宿舍才能删
            model('Room')->delData($ids);
        } catch (\Exception $e) {
            return $this->fail($e->getMessage());
        }
        return $this->ok('删除成功');
    }


    /* ----------------------- 换宿申请 ---------------------------------*/
    public function getApplyList()
    {
        $page = request()->param('page', '1', 'intval');
        $limit = request()->param('limit', 10, 'intval');
        $house_id = $this->house_id();
        $where[] = ['cr.status', 'in', '3,6']; //院长同意3 或 已处理的6
        $where[] = ['cr.house_id', '=', $house_id];
        $list = Db::name('change_room')->alias('cr')
            ->join('member m', 'cr.uid = m.uid')
            ->where($where)->page($page, $limit)
            ->field('m.uid,m.username,m.student_no,m.zhuanye,cr.id,cr.house_id,cr.room_id,cr.reason,cr.create_time,cr.status,cr.teach_remark')
            ->order('cr.id desc')
            ->select();
        foreach ($list as $k => $v) {
            $list[$k]['room'] = model('Room')->where('id', $v['room_id'])->value('title');
            $list[$k]['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
        }
        $total = Db::name('change_room')->alias('cr')
            ->join('member m', 'cr.uid = m.uid')
            ->where($where)->count();
        $data = [
            'list' => $list,
            'total' => $total
        ];
        return $this->ok($data);
    }

    // 更换宿舍  ,成功后修改用户表的楼栋、宿舍为申请表里的字段)，并且宿舍表ha_people字段加1
    public function saveChange()
    {
        $params = request()->only(['id', 'uid', 'room_id']);
        if (!isset($params['id']) || !isset($params['uid'])) {
            return $this->fail('参数错误');
        }
        Db::startTrans();
        try {
            $house_id = $this->house_id();
            //检查宿舍是否满了
            $res = model('Room')->where(['house_id' => $house_id, 'id' => $params['room_id']])->find();
            if ($res['has_people'] == $res['count']) {
                return $this->fail('宿舍已满');
            }
            model('Member')->where(['uid' => $params['uid'], 'house_id' => $house_id])->update(['sushe_id' => $params['room_id'],'house_id' => $house_id]);
            model('Room')->where(['id' => $params['room_id'], 'house_id' => $house_id])->setInc('has_people');
            Db::name('change_room')->where(['id' => $params['id'], 'uid' => $params['uid'], 'house_id' => $house_id])->update(['status' => 6]);
            Db::commit();
            return $this->ok('操作成功');
        } catch (\Exception $e) {
            Db::rollback();
            return $this->fail($e->getMessage());
        }

    }
}